﻿//  Copyright (c) 2009 Ray Liang (http://www.dotnetage.com)
//  Dual licensed under the MIT and GPL licenses:
//  http://www.opensource.org/licenses/mit-license.php
//  http://www.gnu.org/licenses/gpl.html
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DNA.Mvc.UI;


namespace DNA.Mvc.Publishing.Controllers
{
    //[HandlePartialError]
    public partial class ArticleController
    {

        [Widget("Archives",
            "Display an archive list of the current web.",
            Category = "Publishing",
             ShowBorder = true,
             ShowHeader = true,
             ImageUrl = "images/calendar.png",
             IconUrl = "images/calendar_16.png"
            ),
        Property("BlogID")
        ]
        public ActionResult Archives(string blogID)
        {
            var website = _GetWebSiteName();
            if (!string.IsNullOrEmpty(blogID))
                website = blogID;
            ViewData.Model = srv.GetArchives(website);
            return View();
        }

        [Widget("TagCloud", "Display the tag cloud of current web.",
            Category = "Publishing",
             ShowBorder = true,
             ShowHeader = true,
             ImageUrl = "images/tag.png",
             IconUrl = "images/tag_16.png")]
        [Property("MaximumTags", ValueType = typeof(int), DefaultValue = -1)]
        public ActionResult TagCloud()
        {
            int count = 0;
            ViewData.Model = srv.GetTags(_GetWebSiteName(), out count);
            ViewData["Count"] = count;
            return View();
        }


        [Widget("Headlines",
            Category = "Publishing",
            Description = "Display recent articles title link list of specified category.You can use this widget to display the headline news.",
            ShowBorder = true,
            ShowHeader = true,
            ImageUrl = "images/headline.png",
            IconUrl = "images/headline_16.png"
            )]
        [Property("CategoryID", ValueType = typeof(int), DefaultValue = 0)]
        [Property("Rows", ValueType = typeof(int), DefaultValue = 20)]
        [Property("Layout", ValueType = typeof(string), DefaultValue = "_TitleLayout")]
        [Property("TextLen", ValueType = typeof(int), DefaultValue = -1)]
        [Property("ShowSummary", ValueType = typeof(bool), DefaultValue = false, PropertyControl = ControlTypes.Checkbox)]
        [Property("ShowAuthor", ValueType = typeof(bool), DefaultValue = true, PropertyControl = ControlTypes.Checkbox)]
        [Property("ShowDate", ValueType = typeof(bool), DefaultValue = true, PropertyControl = ControlTypes.Checkbox)]
        public ActionResult HeadLines(int categoryID, int rows, bool showAuthor, bool showDate)
        {
            ViewBag.Provider = new CategoryNodeProvider(DependencyResolver.Current.GetService<IPubContext>().Categories);
            Category category = null;
            if (categoryID > 0)
                category = srv.FindCategory(categoryID);

            if (category == null)
                category = srv.FindCategory("home");
            var articles = srv.GetDescendantArticles(category.ID).OrderByDescending(a => a.Posted).Where(a => !a.IsPrivate && a.IsPublished && a.IsAppoved).Take(rows); 
            //var articles = category.Articles.OrderByDescending(a => a.Posted).Where(a => !a.IsPrivate && a.IsPublished && a.IsAppoved).Take(rows);
            return View(articles);
        }

        [Widget("MultiHeadLines",
            Category = "Publishing",
            Description = "Display a multi tab head lines ui.This Widget will load the children categories of specified cateogry and show them in to multi tabs.",
            ShowBorder = true,
            ShowHeader = true,
            ImageUrl = "~/Content/Images/Widgets/multiheadlines.png",
            IconUrl = "~/Content/Images/Widgets/multiheadlines_16.png"
            )]
        [Property("CategoryID", ValueType = typeof(int), DefaultValue = 0)]
        [Property("TextLen", ValueType = typeof(int), DefaultValue = -1)]
        [Property("Rows", ValueType = typeof(int), DefaultValue = 10)]
        [Property("ShowAuthor", ValueType = typeof(bool), DefaultValue = true, PropertyControl = ControlTypes.Checkbox)]
        [Property("ShowDate", ValueType = typeof(bool), DefaultValue = true, PropertyControl = ControlTypes.Checkbox)]
        public ActionResult MultiHeadLines(int categoryID)
        {
            ViewBag.Provider = new CategoryNodeProvider(DependencyResolver.Current.GetService<IPubContext>().Categories);
            var cats = (categoryID <= 0) ? srv.GetChildrenCategories(srv.FindCategory(_GetWebSiteName()).ID) : srv.GetChildrenCategories(categoryID);
            var children = new List<Category>();

            foreach (var c in cats)
            {
                if (c.Articles.Count > 0)
                    children.Add(c);
            }
            return View(children);
        }

        [Widget("HotReads",
            Category = "Publishing",
            Description = "HotReads is a widget that search the most reads article/blog post of the web site and display in a link list."
            )]
        [Property("BlogID")]
        [Property("Rows", ValueType = typeof(int), DefaultValue = 10)]
        public ActionResult HotReads(string blogID, int rows)
        {
            string website = _GetWebSiteName();
            if (!string.IsNullOrEmpty(blogID))
                website = blogID;
            var articles = srv.GetHotPosts(website, rows);
            return View(articles);
        }

        [Widget("RecentBlogPost",
                  Category = "Publishing",
                  Scope = Scopes.Personal,
                  Description = "Display the recent blog posts of current website in a title link list."
                  )]
        [Property("Rows", ValueType = typeof(int), DefaultValue = 10)]
        [Property("TextLen", ValueType = typeof(int), DefaultValue = -1)]
        [Property("ShowAuthor", ValueType = typeof(bool), DefaultValue = true, PropertyControl = ControlTypes.Checkbox)]
        [Property("ShowDate", ValueType = typeof(bool), DefaultValue = true, PropertyControl = ControlTypes.Checkbox)]
        public ActionResult RecentBlogPost(int rows)
        {
            int total = 0;
            return View(srv.GetRecentPosts(0, rows, out total));
        }

        [Widget("BlogRoll",
                  Category = "Publishing",
                  Description = "BlogRoll is a widget that display a list contains the most actively bloggers.")]
        [Property("Rows", ValueType = typeof(int), DefaultValue = 10)]
        public ActionResult BlogRoll(int rows)
        {
            var total = 0;
            return View(srv.GetBlogs(0, rows, out total));
        }

        [Widget("RecentComments",
            Description = "Display the recent comments of current web.",
            Category = "Publishing",
            ShowBorder = true,
            ShowHeader = true,
             ImageUrl = "~/Content/Images/Widgets/comment.png",
             IconUrl = "~/Content/Images/Widgets/comment_16.png"
            )]
        [Property("Rows", ValueType = typeof(int), DefaultValue = 10)]
        public ActionResult RecentComments(int rows)
        {
            var comments = srv.GetRecentComments(_GetWebSiteName(), rows);
            return View(comments);
        }

        [Widget("SummaryView",
            Category = "Publishing",
            ShowHeader = false,
            ShowBorder = false)]
        [Property("CategoryID", ValueType = typeof(int), DefaultValue = 0)]
        [Property("Rows", ValueType = typeof(int), DefaultValue = 20)]
        [Property("Layout", ValueType = typeof(string), DefaultValue = "_TitleLayout")]
        [Property("TextLen", ValueType = typeof(int), DefaultValue = -1)]
        [Property("ShowSummary", ValueType = typeof(bool), DefaultValue = false, PropertyControl = ControlTypes.Checkbox)]
        [Property("ShowAuthor", ValueType = typeof(bool), DefaultValue = true, PropertyControl = ControlTypes.Checkbox)]
        [Property("ShowDate", ValueType = typeof(bool), DefaultValue = true, PropertyControl = ControlTypes.Checkbox)]
        public ActionResult OrderingView(int categoryID, int rows, bool showAuthor, bool showDate)
        {
            ViewBag.Provider = new CategoryNodeProvider(DependencyResolver.Current.GetService<IPubContext>().Categories);
            Category category = null;

            if (categoryID > 0)
                category = srv.FindCategory(categoryID);

            if (category == null)
                category = srv.FindCategory("home");

            var articles = category.Articles.OrderByDescending(a => a.Pos).Where(a => !a.IsPrivate && a.IsPublished && a.IsAppoved).Take(rows);
            return View(articles);
        }

        [Widget("ArticleMenu",
            Category = "Publishing",
            ImageUrl = "images/articlemenu.png",
            IconUrl = "images/articlemenu_16.png",
            ShowHeader = false,
            ShowBorder = true)]
        [Property("CategoryID", ValueType = typeof(int), DefaultValue = 0)]
        [Property("TextLen", ValueType = typeof(int), DefaultValue = -1)]
        public ActionResult Navigator(int categoryID)
        {
            ViewBag.Provider = new CategoryNodeProvider(DependencyResolver.Current.GetService<IPubContext>().Categories);

            var cat = srv.FindCategory(categoryID);
            if (cat != null)
                return View(cat.Articles.OrderBy(a => a.Pos).ToList());
            else
                return View(srv.FindCategory(_GetWebSiteName()).Articles.OrderBy(a => a.Pos).ToList());
        }

        [Widget("MyActions",
            Category = "Publishing",
            Description = "Auto search actions for current user."
         )]
        public ActionResult MyActions()
        {
            ViewBag.TopCategory = srv.FindCategory(_GetWebSiteName());
            return View();
        }

        [Widget("PostRoll",
            Category = "Publishing",
            Description = @"PostRoll is a widget that search and display the latest post and show all the post content in a list.Place this widget on 
top site it will search all the blogger posts in web, when place in your personal web site it just search the posts in your web.",
            ShowHeader = false,
            ShowBorder = false
            )]
        [Property("Rows", ValueType = typeof(int), DefaultValue = 10)]
        public ActionResult PostRoll(int rows)
        {
            var web = WebSiteContext.Current.Web;
            var total = 0;
            if (web.IsRoot && WebSiteContext.Current.EnablePersonalWeb)
                ViewData.Model = srv.GetRecentPosts(0, rows, out total);
            else
                ViewData.Model = srv.GetRecentPosts(_GetWebSiteName(), 0, rows, out total);

            return View();
        }

        [Widget("PostViewer",
            Category = "Publishing",
            Description = "View the exists post content.")]
        [Property("PostID", ValueType = typeof(int), DefaultValue = 0)]
        [Property("ShowTitle", ValueType = typeof(bool), DefaultValue = true)]
        public ActionResult PostViewer(int postID)
        {
            if (postID > 0)
                return View(srv.GetPost(postID));
            return View();
        }
    }
}